/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { getSignConfig, setSignConfig, getSysSignList } from "@/services/messageSignature";
import { message, Modal } from "antd";
import { sliceArrByNum } from "@/utils/utils"

export default {
  namespace: "messageSignature",

  state: {
    data: {},
    notice: null,
    brandList: []
  },

  effects: {
    //获取签名设置
    *getSignConfig({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getSignConfig, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { status } = data

      if (code == 0) {
        if( status && status == 'reject' ){
          yield put({
            type: "save",
            payload: {
              data: {
                ...data,
                sign: "",
                type: "2",
                usage: '1'
              }
            },
          });
        }else{
          yield put({
            type: "save",
            payload: {
              data: {...data}
            },
          });
        }
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    //设置签名
    *setSignConfig({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(setSignConfig, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    //系统签名列表
    *getSysSignList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getSysSignList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { notice, sms_sign } = data
      
      let brandMap = new Map(Object.entries(sms_sign))
      let brandArr = [...brandMap.entries()]
      let slicedArr = sliceArrByNum(brandArr,5)
      let brandList = slicedArr.map((item, index) => {
        const title = []
        const list = []
        for (const [key, value] of item) {
          title.push(key)
          list.push(value)
        }
        return {
          title:title.join(''),
          list
        }
      })

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            notice,
            brandList
          }
        })
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  }
};
